// pages/songDetail/songDetial.js
import PubSub from 'pubsub-js'
import request from '../../utils/request'
import moment from 'moment'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    circleRotateDeg: 0,
    msg: "初始数据",
    song: {},
    startTime:"00:00",
    endTime:"",
    currentTimeProgress:0
  },

  handlePlay() {
    /* 
    
      !注意不要解构赋值后去修改data中的数据 这样不会马上拿到最新更新的数据
    */
   
    this.changePlayState(!this.data.isPlay)

    console.log(this.data.isPlay);
    this.musciControl()
  },
  musciControl() {
    
    if (this.data.isPlay) {
      this.appInstance.appData.currentSongId = this.data.song.id
      this.appInstance.appData.currentSongPlayState = true
      this.audio.src = this.getPlayUrl(this.data.song.id);
      // 标题必填  否则音乐不会播放
      this.audio.title = this.data.song.name;
    } else {
      this.appInstance.appData.currentSongPlayState = false
      this.audio.pause();
    }
  },
  getPlayUrl(id) {
    return `https://music.163.com/song/media/outer/url?id=${id}.mp3`;
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 创建全局实例 防止页面切换导致页面数据丢失  
    this.appInstance = getApp()
    this.audio = wx.getBackgroundAudioManager();

    //监听路由跳转时发射的事件并且拿到数据 
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on("getSongDetail", (res) => {
      if(res.data.id === this.appInstance.appData.currentSongId &&this.appInstance.appData.currentSongPlayState ){
        this.changePlayState(true)
      }
      this.setData({
        song: res.data,
      });
    });
    // 动态修改窗口标题
    wx.setNavigationBarTitle({
      title: this.data.song.name,
    });

    this.audio.onPlay(() => {
      
      this.changePlayState(true)
    })
    this.audio.onPause(() => {
     
      this.changePlayState(false)

    })
    // 播放后 手机上面会有一个停止播放的按钮   监听播放停止的事件
    this.audio.onStop(() => {
      
      this.changePlayState(false)

    })
    // 监听播放进度改变
    this.audio.onTimeUpdate(() => {
      // 
      if(this.appInstance.appData.currentSongId !== this.data.song.id){
        this.setData({
          startTime:"00:00",
          currentTimeProgress:0
        })
      } else {
        let currentTime = this.audio.currentTime
        // console.log(this.audio.duration);
        let width = (this.audio.currentTime/this.audio.duration)*450
        if(this.audio.currentTime == 0){
          width = 0
        }
        this.setData({
          startTime:moment(currentTime*1000).format('mm:ss'),
          currentTimeProgress:width
        })
      }
      
    })
    // 监听播放结束后 自动播放下一曲
    this.audio.onEnded(() => {
      PubSub.publish('getChange','next')
      this.setData({
        currentTimeProgress:0
      })
    })
    
    // 订阅改变歌曲 拿到改变歌曲的id
    PubSub.subscribe('changeSong',(msg,song) => {
      this.setData({
        song
      })
      this.audio.src = this.getPlayUrl(song.id);
      // 标题必填  否则音乐不会播放
      this.audio.title = song.name;
      this.appInstance.appData.currentSongId = song.id
      wx.setNavigationBarTitle({
        title: song.name,
      });
    })
    // 格式化总时长
    this.setData({
      endTime:moment(this.data.song.duration).format('mm:ss')
    })
    
  },
  changePlayState(state){
    this.setData({
      isPlay:state
    })
  },

  changeSong(e){
    let change = e.currentTarget.id
    PubSub.publish('getChange',change)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // PubSub.unsubscribe('changeSong')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    PubSub.unsubscribe('changeSong')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
